package yc.star.forum.controller;

import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.service.UserMessage;
import dev.langchain4j.service.V;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import java.nio.charset.StandardCharsets;
import yc.star.forum.service.aiService.ConsultantService;


@RestController
@RequestMapping("/ai")
public class ChatController {

//    @Autowired
//    private OpenAiChatModel openAiChatModel;
//
//    @GetMapping("/chat")
//    public String chat (String message) {
//        String chat = openAiChatModel.chat(message);
//        return chat;
//    }

    @Autowired
    private ConsultantService consultantService;

//    @GetMapping("/chat")
//    public String chat (String message) {
//        String chat = consultantService.chat(message);
//        return chat;
//    }

    @GetMapping(value = "/chat")
    public ResponseEntity<Flux<String>> chat (String memoryId,String message) {
        // 创建流式响应，确保UTF-8编码
        Flux<String> chatStream = consultantService.chat(memoryId, message);
        
        return ResponseEntity.ok()
                .contentType(new MediaType("text", "event-stream", StandardCharsets.UTF_8))
                .header("Cache-Control", "no-cache")
                .header("Connection", "keep-alive")
                .header("X-Accel-Buffering", "no")
                .header("Access-Control-Allow-Origin", "*")
                .body(chatStream);
    }

}